<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { HomeOutlined, LogoutOutlined, UserOutlined } from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { userLogoutUsingPost } from '@/api/userController.ts'

const loginUserStore = useLoginUserStore()

const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/add_picture',
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: '/admin/pictureManage',
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/admin/spaceManage',
    label: '空间管理',
    title: '空间管理',
  },
]
// 展示在菜单的路由数组
const items = computed<MenuProps['items']>(() => filterMenus(originItems))

const router = useRouter()

//当前需要高亮的菜单项
const current = ref<string[]>([])

//钩子函数，在路由跳转后，更新高亮
router.afterEach((to) => {
  current.value = [to.path]
})

//路由跳转事件(点击菜单栏，跳转到对应的页面)
const doMenuClick = ({ key }) => {
  router.push({
    path: key,
  })
}

//用户登出
const doLogout = async () => {
  const res = await userLogoutUsingPost()
  if (res.data.code == 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push({
      path: '/user/login',
    })
  } else {
    message.error('退出登录失败：' + res.data.message)
  }
}

//用户信息展示
const doUserInfo = async () => {
  await router.push({
    path: '/user/userInfo',
  })
}

// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus.filter((menu) => {
    if (menu.key.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}
</script>

<template>
  <div id="GlobalHeader">
    <a-row :warp="false">
      <a-col flex="150px">
        <RouterLink to="/">
          <div class="title-bar">
            <img alt="logo" class="logo" src="../assets/userAvatar.jpg" />
            <div class="title">ai智能云图库</div>
          </div>
        </RouterLink>
      </a-col>

      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          :items="items"
          mode="horizontal"
          @click="doMenuClick"
        />
      </a-col>

      <a-col flex="120px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <a-space>
                <a-avatar :src="loginUserStore.loginUser.userAvatar" />
                {{ loginUserStore.loginUser.userAccount ?? '无名' }}
              </a-space>
              <template #overlay>
                <a-menu>
                  <a-menu-item @click="doUserInfo">
                    <a>个人信息</a>
                    <UserOutlined />
                  </a-menu-item>
                  <a-menu-item>
                    <router-link to="/my_space">
                      我的空间
                      <UserOutlined />
                    </router-link>
                  </a-menu-item>
                  <a-menu-item @click="doLogout">
                    <a>退出登录</a>
                    <LogoutOutlined />
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button href="/user/login" type="primary">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<style scoped>
#GlobalHeader .title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 16px;
  margin-left: 16px;
}

.logo {
  height: 30px;
}
</style>
